package com.red.wood.module.login;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.dubbo.config.annotation.Reference;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.module.api.service.ApiService;
import com.thinkgem.jeesite.common.service.AuthorityDataService;
import com.thinkgem.jeesite.modules.sys.entity.Menu;
import com.thinkgem.jeesite.modules.sys.entity.User;

@Controller
public class LoginController{
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Reference
	private AuthorityDataService authorityDataService;
	
	@Autowired
	private ApiService service;
	
	/**
	 * 管理登录
	 * 新4A配置方式：部署名称配置区别
	 * 系统名称	      系统简称	               访问地址	                   访问端口	部署名称	
       API服务调用      api-web	  http://localhost	    8092	api-web/index_api
	 */
	@RequestMapping(value = "index_api")
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response, Model model) {
		ModelAndView mv = new ModelAndView("home");
		try {
			String token = CommonUtils.valueOf(request.getParameter("token"));
			String subsysId = CommonUtils.valueOf(request.getParameter("appkey"));
			String name = CommonUtils.valueOf(request.getParameter("usr"));
			
			StringBuffer categoryNameStr = new StringBuffer();
			StringBuffer serviceIdStr = new StringBuffer();
			Map<String,List<String>> mapApi = new HashMap<String,List<String>>();
			if(StringUtils.isNotBlank(token)) {//用户可以不通过4A,直接传入token值
				List<Map<String,Object>> serviceList = service.getApiServiceInfoByToken(token);
				if(!serviceList.isEmpty()) {
					for(Map<String,Object> map : serviceList) {
						categoryNameStr.append("'").append(map.get("category_name")).append("'").append(",");
						serviceIdStr.append(map.get("service_id")).append(",");
					}
				}
				request.getSession().setAttribute("is_public","yes");
			}else {//通过4A配置获取服务
				User user = authorityDataService.getUser(subsysId, name);
				if(user != null) {
					List<Menu> rootMenu = user.getRootMenu().getChildList().get(0).getChildList();
					for(Menu menu : rootMenu) {
						if(StringUtils.isNotBlank(menu.getName())) {
							categoryNameStr.append("'").append(menu.getName()).append("'").append(",");
						}
						List<Menu> childList = menu.getChildList();
						List<String> list = new ArrayList<String>();
						for(Menu m : childList) {
							if(StringUtils.isNotBlank(m.getHref())) {
								serviceIdStr.append(m.getHref()).append(",");
								list.add(m.getHref());
							}
						}
						mapApi.put(menu.getName(),list);
					}
				}
			}
			if(categoryNameStr.length() > 0) {
				request.getSession().setAttribute("categoryNameStr",categoryNameStr.substring(0, categoryNameStr.length()-1));
				request.getSession().setAttribute("apiInfo",mapApi);
			}else {
				request.getSession().setAttribute("categoryNameStr","");
			}
			if(serviceIdStr.length() > 0) {
				request.getSession().setAttribute("serviceIdStr",serviceIdStr.substring(0, serviceIdStr.length()-1));
			}else {
				request.getSession().setAttribute("serviceIdStr","");
			}
			request.getSession().setAttribute("user",name);
			logger.info("categoryNameStr："+categoryNameStr);
			logger.info("serviceIdStr："+serviceIdStr);
		}catch(Exception e) {
			logger.error(e.getMessage(),e);
		}
		return mv;
	}
}